package com.woniu.atms.controller.aft;

import com.github.pagehelper.PageInfo;
import com.woniu.atms.entity.G5Car;
import com.woniu.atms.service.CarService;
import com.woniu.atms.util.Code;
import com.woniu.atms.util.ResponseResult;
import com.woniu.atms.validation.AddCar;
import com.woniu.atms.vo.PageInfoVO;
import com.woniu.atms.vo.aft.car.CarInfoVO;
import com.woniu.atms.vo.aft.car.CarStatusVo;
import com.woniu.atms.vo.aft.car.CarTypeIdVO;
import com.woniu.atms.vo.aft.site.group.AddGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 张奇
 * @title: AftCarController 车辆管理
 * @projectName g5_atms
 * @description: 车辆管理
 * @date 2022/1/17 15:19
 */
@Api(tags = "车辆管理")
@RestController
@RequestMapping("/api/aft/v1/car")
public class AftCarController {

    @Resource
    private CarService carService;

    @ApiOperation(value = "车辆列表查询",notes = "查询所有车辆列表")
    @RequestMapping("/list")
    public ResponseResult carList(@RequestBody @Valid PageInfoVO vo){
        PageInfo<G5Car> cars=carService.queryAll(vo.getPageNum(),vo.getPageSize());
        return  new ResponseResult(Code.SUCCESS, "查询车辆列表成功", cars);
    }

    @ApiOperation(value = "车辆列表模糊查询",notes = "多条件模糊查询查询所有车辆列表")
    @RequestMapping("/list_car")
    public ResponseResult queryToCar(@RequestBody @Valid CarInfoVO vo){
        PageInfo<G5Car> cars=carService.queryToCar(vo.getCar(),vo.getPageInfoVO().getPageNum(),vo.getPageInfoVO().getPageSize());
        return  new ResponseResult(Code.SUCCESS, "查询车辆列表成功", cars);
    }

    @ApiOperation(value = "车辆具体信息查询",notes = "通过id查询")
    @GetMapping("/info/{id}")
    public ResponseResult carInfo(@PathVariable("id") Integer id){
        G5Car car = carService.queryById(id);
        if(car!=null){
           return new ResponseResult<>(Code.SUCCESS, "查询车辆信息成功", car);
        }else {
            return new ResponseResult<>(Code.FAIL, "查询车辆信息失败");
        }
    }
    @ApiOperation(value = "车辆列表查询",notes = "通过车辆类型查询")
    @GetMapping("/list_type")
    public ResponseResult carListByType(@RequestBody @Valid CarTypeIdVO vo){
        PageInfo<G5Car> cars=carService.queryByType(vo.getTypeId(),vo.getPageInfoVO().getPageNum(),vo.getPageInfoVO().getPageSize());
        return  new ResponseResult(Code.SUCCESS, "查询车辆列表成功", cars);
    }

    @ApiOperation(value = "车辆列表查询",notes = "通过车辆启用状态查询")
    @GetMapping("/list_status")
    public ResponseResult carListByStatus(@RequestBody @Valid CarStatusVo vo){
        PageInfo<G5Car> cars=carService.queryByStatus(vo.getStatus(),vo.getPageInfoVO().getPageNum(),vo.getPageInfoVO().getPageSize());
        return  new ResponseResult<>(Code.SUCCESS, "查询车辆列表成功", cars);
    }

    @ApiOperation(value = "添加车辆信息",notes = "添加车辆信息")
    @PostMapping("/add")
    public ResponseResult<Void> addCar(@RequestBody @Validated(AddCar.class) G5Car car){
        boolean flag = carService.insert(car);
        if (flag) {
            return ResponseResult.SUCCESS;
        }else {
            return ResponseResult.FAIL;
        }
    }

    @ApiOperation(value = "修改车辆信息",notes = "需要修改的car")
    @PostMapping("/update")
    public ResponseResult<Void> updateStatus(@RequestBody G5Car car){
        boolean flag = carService.update(car);
        if (flag) {
            return ResponseResult.SUCCESS;
        }else {
            return ResponseResult.FAIL;
        }
    }

    @ApiOperation(value = "通过id删除车辆",notes = "通过id删除车辆，逻辑删除")
    @GetMapping("/delete/{id}")
    public ResponseResult<Void> deleteCar(@PathVariable("id") Integer id){
        boolean flag = carService.deleteById(id);
        if (flag) {
            return ResponseResult.SUCCESS;
        }else {
            return ResponseResult.FAIL;
        }
    }



}
